package com.itSession.cn.controller.app;

import com.itSession.cn.entity.UserInfo;
import com.itSession.cn.exception.ResponseData;
import com.itSession.cn.mapper.UserInfoMapper;
import com.itSession.cn.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;


@Api(tags = "APP登录模块")
@RequestMapping("/app/comments")
@RestController
@MapperScan("com.itSession.cn.mapper")
public class DemoLoginController {


    @Autowired
    private UserInfoMapper userInfoMapper;


    @ApiOperation(value = "登录", httpMethod = "POST", notes = "加载数据", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "账号", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "demoUserPwd", value = "密码", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "demoUserRole", value = "角色", required = false, paramType = "query", dataType = "string"),
    })
    @RequestMapping("login")
    public ResponseData listAll(@ApiIgnore String userAccount, @ApiIgnore String demoUserPwd,@ApiIgnore String demoUserRole) {
        try {
            UserInfo user = userInfoMapper.getUserByAccount(userAccount);
            if (demoUserPwd.equals(user.getUserPwd()) && demoUserRole.equals(user.getUserRole())) {
                return new ResponseData().data(user).success();
            }
            return new ResponseData().fail(10003, "用户名或密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData().fail(10002, "用户不存在");
        }
    }


    /**
     * 注册
     *
     * @param demoUser 注册的记录
     * @return 返回影响行数
     */
    @RequestMapping("registered")
    public ResponseData registered(@ApiIgnore UserInfo demoUser) {
        try {
            UserInfo user = userInfoMapper.getUserByAccount(demoUser.getUserAccount());
            if (null == user) {
                userInfoMapper.insertIgnoreNull(demoUser);
            } else {
                return new ResponseData().fail(10001, "账号已存在，请更换。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData().fail(500, e.getMessage());
        }
        return new ResponseData().success();
    }


}
